#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <map>
#include <climits>

using namespace std;

vector<int> split(const string &str,const string &dli){
    vector<int> res;
    if(str.size()==0) return res;
    char *strs=new char[str.length()+1];
    strcpy(strs,str.c_str());
    char *dstr=new char[dli.length()+1];
    strcpy(dstr,dli.c_str());
    char *p=strtok(strs,dstr);
    while(p){
        string s=p;
        res.push_back(stoi(s));
        p=strtok(NULL,dstr);
    }
    return res;
}


int C;
vector<int> vsv,wsv;
int main(){
    // freopen("in.txt","r",stdin);
    string vs;
    string ws;
    cin >> C;
    cin >> ws;
    cin >> vs;
    wsv=split(ws,","); //重量
    vsv=split(vs,","); //价值
    vector<int> dp(C+5,0);
    int n=wsv.size();
    for(int i=0;i<n;i++){
        for(int j=C;j>=wsv[i];j--){
            dp[j]=max(dp[j],dp[j-wsv[i]]+vsv[i]);
        }
    }
    // for(int i=0;i<=n;i++){
    //     cout << dp[i] << " "; 
    // }cout <<endl;

    cout << dp[C] <<endl;



    return 0;
}